#from knowledge_based_chatglm import answer_chatglm, answer_chatglm_private

import gradio as gr

from Server import  chatglmpro, qianwen7b, qianwen, enire, chatgpt3, chatgpt4


#这是开启前端界面
def answer(ask,history,choose,choose_model):
    if len(history)>16:
        history.pop(0)
 
    if choose=="chatglm-6b-int4":
        return  botmodel(chatglm6b,choose_model,ask,history)
    elif choose=="chatglm-pro":
        return botmodel(chatglmpro,choose_model,ask,history)
    elif choose=="文心一言":        return  botmodel(enire,choose_model,ask,history)
    elif choose=="通义千问-7b":
        return botmodel(qianwen7b,choose_model,ask,history)
    elif choose=="通义千问":
        return botmodel(qianwen,choose_model,ask,history)
    elif choose=="chatgpt3":
        return botmodel( chatgpt3,choose_model,ask,history)
    elif choose=="chatgpt4":
        return  botmodel( chatgpt4,choose_model,ask,history)


def botmodel(chatbot,choose_model,ask,history):
    if choose_model == "原生":
        return chatbot.answer_org(ask, history)
    elif choose_model == "助手":
        return chatbot.answer(ask, history)
    elif choose_model == "AI咨询师":
        return chatbot.answer_private(ask, history)




def startgr(port):
    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        msg = gr.Textbox()
        choose = gr.inputs.Dropdown(["chatglm-6b-int4","chatglm-pro", "文心一言", "通义千问-7b", "通义千问", "chatgpt3", "chatgpt4"], label="选择一个聊天机器人", default="chatglm-pro")
        choose_model = gr.inputs.Dropdown(["原生",  "助手", "AI咨询师"], label="选择一个聊天机器人", default="原生")

        def respond(message,history,choose,choose_model):
            allres = answer(message,history,choose,choose_model)
            for chat_history in allres:
                yield "", chat_history

        msg.submit(respond, [msg, chatbot, choose,choose_model], [msg, chatbot])

    demo.launch(server_name="0.0.0.0", server_port=port,enable_queue=True)


history = []
startgr(7860)